/**
 * This file is a part of qloudgen-rul. 
 * You can redistribute qloudgen-rul and/or modify it under the terms of the Lesser GNU General Public License version 3. 
 * qloudgen-rul is distributed WITHOUT ANY WARRANTY. 
 *
 * See the Lesser GNU General Public License for more details at http://www.gnu.org/licenses/. 
 **/
package org.qloudgen.rul.curator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.qloudgen.sec.util.ExceptionUtil;

/**
 * <p>Title: Curator Framework Wrapper</p>
 * <p>Description: Wrapper repository cache sender implementation.</p>
 * <p>Copyright: qloudgen/rul 2014</p>
 * @author <a href="mailto:hyq.dd.sun@gmail.com">glue.3M</a>
 */
public class RepositoryPathCacheSender extends AbstractPathCacheClient {
	/**
	 * Log output.
	 */
	private final static Logger logger = LoggerFactory.getLogger( RepositoryPathCacheSender.class );
	
	/**
	 * Default constructor.
	 */
	public RepositoryPathCacheSender() {
		//super();
	}
	
	/**
	 * Custom constructor.
	 *
	 * @param root
	 * @param configFile
	 */
	public RepositoryPathCacheSender(String root , String configFile) {
		super( root , configFile );
	}
	
	/**
	 * Added child.
	 *
	 * @param path
	 * @param data
	 */
	public void addChild(String path , byte[] data) {
		try {
			getClient().create().creatingParentsIfNeeded().forPath( path , data );
		}
		catch (Throwable t) {
			logger.error( "Current repository cache add child error...\n{}" , ExceptionUtil.get( t ) );
			throw new CuratorException( t );
		}
	}
	
	/**
	 * Updated child.
	 *
	 * @param path
	 * @param data
	 */
	public void updateChild(String path , byte[] data) {
		try {
			getClient().setData().forPath( path , data );
		}
		catch (Throwable t) {
			logger.error( "Current repository cache update child error...\n{}" , ExceptionUtil.get( t ) );
			throw new CuratorException( t );
		}
	}
	
	/**
	 * Removed child.
	 *
	 * @param path
	 */
	public void removeChild(String path) {
		try {
			getClient().delete().forPath( path );
		}
		catch (Throwable t) {
			logger.error( "Current repository cache remove child error...\n{}" , ExceptionUtil.get( t ) );
			throw new CuratorException( t );
		}
	}
	
	/**
	 * Returns the String representation.
	 *
	 * @return String
	 */
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append( "[" );
		builder.append( super.toString() );
		builder.append( "]" );
		//
		return builder.toString();
	}
}